package com.ruoyi.web.controller.tool;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PdfUtil {
    public static void mergePdfFiles(List<String> sourcePaths, String destinationPath)
            throws IOException, DocumentException {
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(destinationPath));
        document.open();

        for (String sourcePath : sourcePaths) {
            PdfReader reader = new PdfReader(sourcePath);
            int numPages = reader.getNumberOfPages();
            for (int i = 1; i <= numPages; i++) {
                copy.addPage(copy.getImportedPage(reader, i));
            }
            reader.close();
        }

        document.close();
    }

    /**
     * 拆分 PDF 文件为多个文件
     *
     * @param sourcePath       待拆分的 PDF 文件路径
     * @param destinationPaths 拆分后的 PDF 文件路径列表
     * @throws IOException       IO 异常
     * @throws DocumentException PDF 文档异常
     */
    public static void splitPdfFile(String sourcePath, List<String> destinationPaths)
            throws IOException, DocumentException {
        PdfReader reader = new PdfReader(sourcePath);

        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            Document document = new Document();
            PdfCopy copy = new PdfCopy(document, new FileOutputStream(destinationPaths.get(i - 1)));
            document.open();
            copy.addPage(copy.getImportedPage(reader, i));
            document.close();
        }

        reader.close();
    }

    /**
     * 提取 PDF 文件的指定页面为新文件
     *
     * @param sourcePath      待提取页面的 PDF 文件路径
     * @param destinationPath 提取后的页面文件路径
     * @param pageNumber     要提取的页面页码（从1开始）
     * @throws IOException       IO 异常
     * @throws DocumentException PDF 文档异常
     */
    public static void extractPageFromPdf(String sourcePath, String destinationPath, int pageNumber)
            throws IOException, DocumentException {
        PdfReader reader = new PdfReader(sourcePath);
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(destinationPath));
        document.open();
        copy.addPage(copy.getImportedPage(reader, pageNumber));
        document.close();
        reader.close();
    }


    /**
     * 合并pdf
     * @param files 需要合并的pdf路径
     * @param newfile 合并成新的文件的路径
     * @return
     */
    public static boolean mergePdfFiles(String[] files, String newfile) {
        boolean retValue = false;
        Document document = null;
        PdfCopy copy = null;
        PdfReader reader = null;
        try {
            document = new Document(new PdfReader(files[0]).getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(newfile));
            document.open();
            for (int i = 0; i < files.length; i++) {
                reader = new PdfReader(files[i]);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }
            retValue = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (copy != null) {
                copy.close();
            }
            if (document != null) {
                document.close();
            }
        }
        return retValue;
    }
    public static void TextWordToPdf(String wordPath, String pdfPath) throws Exception {

    }


    public static void main(String[] args) {
       /* String[] files = { "C:\\Users\\Administrator\\Desktop\\然管\\盖章资料\\01-保密责任书.pdf",
                "C:\\Users\\Administrator\\Desktop\\然管\\盖章资料\\02-工作联系函.pdf",
                 "C:\\Users\\Administrator\\Desktop\\然管\\盖章资料\\03-设备报审表.pdf",
                "C:\\Users\\Administrator\\Desktop\\然管\\盖章资料\\04-设备进场查验记录单.pdf",
                "C:\\Users\\Administrator\\Desktop\\然管\\盖章资料\\05-设备开箱记录单.pdf",
                "C:\\Users\\Administrator\\Desktop\\然管\\盖章资料\\06-设备加电测试记录单.pdf",
                "C:\\Users\\Administrator\\Desktop\\然管\\盖章资料\\08-会议纪要.pdf",
                "C:\\Users\\Administrator\\Desktop\\然管\\盖章资料\\09-项目完工报告.pdf",
        };
        String savepath = "C:\\Users\\Administrator\\Desktop\\然管\\盖章资料\\temp.pdf";

        boolean b = mergePdfFiles(files, savepath);
        System.out.println(b);
    }*/
        try {
            List<String> sourcePaths = new ArrayList<>();
            sourcePaths.add("path/to/source1.pdf");
            sourcePaths.add("path/to/source2.pdf");
            String mergedFilePath = "path/to/merged.pdf";

            // 合并多个 PDF 文件示例
            PdfUtil.mergePdfFiles(sourcePaths, mergedFilePath);
            System.out.println("PDF 文件合并完成，合并后的文件路径：" + mergedFilePath);

            // 拆分 PDF 文件示例
            String sourceFilePath = "path/to/source.pdf";
            List<String> splitFilePaths = new ArrayList<>();
            splitFilePaths.add("path/to/split1.pdf");
            splitFilePaths.add("path/to/split2.pdf");
            PdfUtil.splitPdfFile(sourceFilePath, splitFilePaths);
            System.out.println("PDF 文件拆分完成，拆分后的文件路径：" + splitFilePaths);

            // 提取 PDF 页面示例
            String sourceFile = "path/to/source.pdf";
            int pageNumber = 2;
            String extractedPageFile = "path/to/extractedPage.pdf";
            PdfUtil.extractPageFromPdf(sourceFile, extractedPageFile, pageNumber);
            System.out.println("PDF 页面提取完成，提取后的页面文件路径：" + extractedPageFile);

        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
    }

}
